import router from './index'
import ngProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '../store/index'
import { menuList } from '@/config/menu'
const obj = menuList.reduce((pre, cur) => {
  // @ts-ignore
  pre[cur.href] = cur.num
  return pre
}, {})

router.beforeEach((to, from, next) => {
  ngProgress.start()
  if (Object.keys(obj).includes(to.path)) {
    // @ts-ignore
    store.commit('SET_INDEX', obj[to.path])
  }
  if ((store.state as any).auth.authInfo.code === 1) {
    next()
  } else {
    if (to.path === '/') {
      next()
    } else {
      next('/')
    }
  }
})

router.afterEach((to, from) => {
  ngProgress.done()
})
